package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.TransferRecord;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.param.SendUserDropsParam;
import com.yami.shop.bean.param.UserUpdateParam;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.StringUtils;
import com.yami.shop.service.BusinessNftService;
import com.yami.shop.service.SysConfigService;
import com.yami.shop.service.TransferRecordService;
import com.yami.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/transferRecord")
public class TransferRecordController {

    @Autowired
    private TransferRecordService transferRecordService;
    @Autowired
    private UserService userService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private BusinessNftService businessNftService;


    /**
     * 分页查询所有数据
     *
     * @param page                  分页对象
     * @param airdropTaskConditions 查询实体
     * @return 所有数据
     */
    @GetMapping
    public ServerResponseEntity<List<TransferRecord>> selectAll(Page<TransferRecord> page, TransferRecord airdropTaskConditions) {
        Page<TransferRecord> page1 = this.transferRecordService.page(page, new QueryWrapper<>(airdropTaskConditions));
        return ServerResponseEntity.success(page1.getRecords());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ServerResponseEntity<TransferRecord> selectOne(@PathVariable Serializable id) {
        return ServerResponseEntity.success(this.transferRecordService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param airdropTaskConditions 实体对象
     * @return 新增结果
     */
    @PostMapping
    public ServerResponseEntity<Boolean> insert(@RequestBody TransferRecord airdropTaskConditions) {
        return ServerResponseEntity.success(this.transferRecordService.save(airdropTaskConditions));
    }

    /**
     * 修改数据
     *
     * @param airdropTaskConditions 实体对象
     * @return 修改结果
     */
    @PutMapping
    public ServerResponseEntity<Boolean> update(@RequestBody TransferRecord airdropTaskConditions) {
        // 判断用户是否存在
        User userDetailByCallPhone = userService.getUserDetailByCallPhone(airdropTaskConditions.getPhone());
        if(Objects.isNull(userDetailByCallPhone)){
            return ServerResponseEntity.showFailMsg("该用户不存在");
        }
        if(StringUtils.isEmpty(userDetailByCallPhone.getIdNumber())){
            return ServerResponseEntity.showFailMsg("该账号未实名");
        }
        if(userDetailByCallPhone.getIdNumber().equals(airdropTaskConditions.getIdNum())){
            return ServerResponseEntity.showFailMsg("该账号实名信息不符合");
        }
        // 空投
        // 获取空头商品
        if("1".equals(airdropTaskConditions.getStatus())){
            String prodId = sysConfigService.getValue("KT_PROD_ID");
            UserUpdateParam param = new UserUpdateParam();
            List<SendUserDropsParam> userDropsParams = new ArrayList<>();
            SendUserDropsParam sendUserDropsParam = new SendUserDropsParam();
            sendUserDropsParam.setTargetId(Long.parseLong(prodId));
            sendUserDropsParam.setType(0);
            sendUserDropsParam.setNums(airdropTaskConditions.getNum().intValue());
            userDropsParams.add(sendUserDropsParam);
            param.setDrops(userDropsParams);
            param.setUserIds(Collections.singletonList(userDetailByCallPhone.getUserId()));
            param.setBusinessType(99);
            param.setMainProdId(Long.parseLong(prodId));
            param.setAirdropBusinessId(airdropTaskConditions.getId());
            ShopDetail shopDetail = new ShopDetail();
            shopDetail.setShopId(999L);
            businessNftService.bindDropById(param, shopDetail);
        }


        return ServerResponseEntity.success(this.transferRecordService.updateById(airdropTaskConditions));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public ServerResponseEntity<Boolean> delete(@RequestParam("idList") List<Long> idList) {
        return ServerResponseEntity.success(this.transferRecordService.removeByIds(idList));
    }

}
